package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {
    @Reference
    private OrderSettingService orderSettingService;
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile xlsFile) throws IOException {
        try {
            List<String[]> strings = POIUtils.readExcel(xlsFile);
            Result upload = orderSettingService.upload(strings);
            return upload;
        }catch (Exception e){
            return new Result(false,MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }

    }

    @RequestMapping("/download")
    public void download(HttpServletResponse response) throws IOException {
//        https://blog.csdn.net/hehuihh/article/details/91318662
        File file=new File("G:\\dev\\IdeaProjects\\my_health\\health_parent\\health_backend\\src\\main\\webapp\\template\\ordersetting_template.xlsx");
        String fileName = file.getName();
        String contentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        String contentDisposition="attachment;filename="+fileName;
        response.setHeader("Content-Type", contentType);
        response.setHeader("Content-Disposition", contentDisposition);
        FileInputStream fis = new FileInputStream(file);
        int b = 0;
        byte[] buffer = new byte[512];
        ServletOutputStream os = response.getOutputStream();
        while (b!=-1){
            b=fis.read(buffer);
            os.write(buffer,0,b);
        }
        fis.close();
    os.flush();
    }

//    @RequestMapping("/findData/{year}/{month}")
//    public PageResult findData(@PathVariable("year") String year, @PathVariable("month") String month){
//        System.out.println(year+" "+month);
//        PageResult data = orderSettingService.findData(year, month);
//        return data;
//    }
@RequestMapping("/findData")
public Result findData(String date){
    try {
        List<Map> list = orderSettingService.getOrderSettingsByDate(date);
        return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,list);
    } catch (Exception e) {
        e.printStackTrace();
        return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
    }

}
    @RequestMapping("/edit")
    public Result edit(@RequestBody OrderSetting orderSetting){
        System.out.println(orderSetting);
        try {
            orderSettingService.edit(orderSetting);
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }

    }

    public static void main(String[] args) {

        Date date = new Date(2020-1900,1,29);
//        java.sql.Date date1 = new java.sql.Date(2020,1,30);
//        System.out.println(date1);
        System.out.println(date);
//        System.out.println(date.getYear());
//        date.setYear(2020);
//        date.setMonth(1);
//        date.setDate(30);
//        System.out.println(date);
    }
}
